/**
 * -----------------------------------
 * Copyright (c) 2021-2024
 * All rights reserved, Designed By linfeng.tech , linfengtech.cn
 * 林风婚恋交友开源版本请务必保留此注释头信息
 * 开源地址: https://gitee.com/yuncoder001/linfeng-love.git
 * 商业版授权联系技术客服	 QQ:  973921677/3582996245
 * 严禁分享、盗用、转卖源码或非法牟利！
 * 版权所有 ，侵权必究！
 * -----------------------------------
 */
package com.xyz.frame.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyz.frame.common.response.PageObject;
import com.xyz.frame.dao.UserXqMapper;
import com.xyz.frame.bean.model.User;
import com.xyz.frame.service.UserXqService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class UserXqServiceImpl
        extends ServiceImpl<UserXqMapper, User> implements UserXqService {

    private static final String CACHE_USER_UID_PREFIX = "uid:";
//    private final RedisUtil redisUtils;

//    public UserServiceImpl(RedisUtil redisUtils) {
//        this.redisUtils = redisUtils;
//    }

    @Override
    public PageObject queryPage(Map<String, Object> params) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //模糊查询
        String key = (String) params.get("key");

        if (!StringUtils.isEmpty(key)) {
            if (NumberUtil.isInteger(key)) {
                queryWrapper.lambda().eq(User::getUid, key);
            }
            queryWrapper.lambda().like(User::getUserName, key)
                    .or().like(User::getMobile, key);
        }

        queryWrapper.lambda().orderByDesc(User::getUid);
        long pageSize = Long.parseLong((String)params.get("limit"));
        long pageNum = Long.parseLong((String)params.get("page"));;
        IPage<User> page = this.page(new Page<>(pageNum, pageSize), queryWrapper);
        //如果不需要对手机号打码就注释掉下面这段
        List<User> records = page.getRecords();
        records.forEach(user -> {
            if (!StringUtils.isEmpty(user.getMobile())) {
//                user.setMobile(StringUtils.maskMobile(user.getMobile()));
                user.setMobile(user.getMobile());
            }
        });
        page.setRecords(records);


        return new PageObject(page);
    }

    @Override
    public void updateAndDeleteCache(User user) {
        user.setUpdateTime(new Date());
        this.updateById(user);
//        redisUtils.delete(CACHE_USER_UID_PREFIX + user.getUid());
    }

    @Override
    public User getUserByUid(String uid) {
//        User userInfo = redisUtils.get(CACHE_USER_UID_PREFIX + uid, User.class);
//        if (userInfo != null) {
//            return userInfo;
//        }
        //重新获取用户信息
        User user = this.getById(uid);
//        redisUtils.set(CACHE_USER_UID_PREFIX + uid, user, 7200);
        return user;
    }

}